<?php
/**
 * Created by PhpStorm.
 * User: zl
 * Date: 2017/5/11
 * Time: 23:12
 * 此PHP作为通用的上传文件PHP使用
 */
    require_once ('Common.php');
    require_once ('SQLBase.php');
    require_once ('Files.php');
    //获取所有上传文件
    $files = $_FILES;
    //获取申请类型
    $type = $_REQUEST['type'];
    //获取申请ID
    $id = $_REQUEST['id'];
    //构造返回数据
    $return_array = array('error_num'=> 0, "error_state"=>'');
    $f = new Files();
    $fileURL = $f->createFilePath($type, $id);

    //如果文件夹不存在，创建
    if(!is_dir($fileURL)){
       if(!$f->createDirectory($fileURL)){
           $return_array['state'] = 0;
           $return_array['error_num'] = 1;
           $return_array['error_state'] = 'FAILE_TO_CREATE_FOLDER';
           echo json_encode($return_array);
           exit();
       }
    }
    else{  //如果文件夹存在，删除里面的所有文件
        $f->delFile($fileURL);
    }
    $all_files = '';
    if(!($all_files = $f->uploadFile($files, $fileURL))){
        $return_array['state'] = 0;
        $return_array['error_num'] = 2;
        $return_array['error_state'] = 'FAILE_TO_UPLOAD_FILE';
        echo json_encode($return_array);
        exit();
    }

    $r = $f->uploadFileState($id, $all_files, $type);

    if($r){
        $return_array['state'] = 1;
    }else{
        $return_array['state'] = 0;
        $return_array['error_num'] = 3;
        $return_array['error_state'] = 'FAILE_TO_WRITE_DB';
    }
    echo json_encode($return_array);




?>
